<template>
    <div>
        <!-- 面包屑导航区域 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb> 
        <!-- 卡片 -->
        <el-card>
            <el-row>
                <el-col>
                    <el-input placeholder="请输入内容" v-model="queryInfo.query" class="el-btn" clearable @clear="getGoodsListData" >
                        <el-button slot="append" icon="el-icon-search" @click="getGoodsListData"></el-button>
                    </el-input>
                    <el-button type="primary" @click="addGoods">添加商品</el-button>
                </el-col>
            </el-row>
            <!--商品列表 区域 -->
            <el-table :data="goodsList" border stripe>
                <el-table-column type="index" label="#"></el-table-column>
                <el-table-column label="商品名称" prop="goods_name"></el-table-column>
                <el-table-column label="价格" prop="goods_price" width="100px"></el-table-column>
                <el-table-column label="添加时间"  width="160px">
                    <template slot-scope="scope"> {{scope.row.add_time | dateFormat}} </template>
                </el-table-column>
                <el-table-column label="操作"  width="150px">
                    <template slot-scope="scope">
                        <el-button type="primary" icon="el-icon-edit" ></el-button>
                        <el-button type="danger" icon="el-icon-delete" @click="removeById(scope.row.goods_id)"></el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页区域 -->
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum"
                :page-sizes="[50, 100, 150, 200]"
                :page-size="100"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total" background>
            </el-pagination>
        </el-card>
    </div>
</template>

<script>
export default {
    name:'GoodsList',
    data(){
        return {
            // 商品数据 对象
            queryInfo:{
                query:'',
                pagenum:1,
                pagesize:10
            },  
            // 存储商品列表 数组
            goodsList:[],
            // 总数
            total:0
        }
    },
    created(){
        this.getGoodsListData();
    },
    methods:{
        async getGoodsListData(){
            const {data:res} = await this.$http.get('/goods',{
                params:this.queryInfo
            });
            if(res.meta.status !== 200){
                return this.$message.error('获取商品列表数据失败！');
            }
            // this.$message.success('获取商品列表数据成功！');
            this.goodsList = res.data.goods;
            this.total = res.data.total;
            console.log(res.data,"商品数据");
        },
        // 当前页面数据条数 发生改变时
        handleSizeChange(newsize){
            this.queryInfo.pagesize = newsize;
            this.getGoodsListData();
        },
        // 当前页面的页码 发生变化时
        handleCurrentChange(newPage){
            this.queryInfo.pagenum = newPage;
            this.getGoodsListData();
        },
        // 添加商品 信息
        addGoods(){
            this.$router.push('/goods/add');
        },
        // 删除商品
        async removeById(id){ 
            const confirmResult = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).catch(error => error);

            if(confirmResult !== 'confirm'){
                return this.$message.error('已经取消删除！');
            }
            
            const {data:res} = await this.$http.delete(`/goods/${id}`);
            if(res.meta.status !== 200){
                return this.$message.error('商品删除失败！');
            }
            this.$message.success('商品删除成功！');
            this.getGoodsListData();
        }
    }
}
</script>

<style lang="less" scoped>
.el-btn{
    width: 300px;    
    margin-right: 15px;
}
.el-table{
    margin: 15px 0;
}
</style>
